1136D - Nastya Is Buying Lunch - CodeForces Solution


greedy *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf (ll)1e18
#define pi (3.141592653589)
ll mod=1000000007;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
vector<ll>adj[300002];
vector<ll>vis(200002,0);
vector<ll>dis(200002,0);
#define countbit(a) __builtin_popcount(a)
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}  //for prime b
//void google(int t) {cout << Case # << t << : ;}
#define all(v) v.begin(),v.end()
/*returns nCr*/ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
#define vll vector<ll>
#define pll pair<ll,ll>
#define dec(n) cout<<fixed; cout<<setprecision(n);
ll dp1[1030][1020];
ll C(int n, int r){ if (r == 0 || r == n) return 1; if (dp1[n][r]) return dp1[n][r];return dp1[n][r] = (C(n - 1, r - 1) + C(n - 1, r)) % mod;}
/*ll up[200002][20];
ll depth[200002];
ll LCA(ll x,ll y){if(depth[x]>depth[y]) swap(x,y); ll d= depth[y]-depth[x];  for(int i=19;i>=0;i--){ if(d&(1<<i)) {y=up[y][i];}}if(x==y) return x; for(int i=19;i>=0;i--) {if(up[x][i]!=up[y][i]){x=up[x][i];y=up[y][i];}}return up[x][0];}
void LCAprec(ll node){vis[node]=1;for(auto x:adj[node]){if(vis[x]==0){depth[x]=depth[node]+1;up[x][0]=node;for(int i=1;i<20;i++){up[x][i]=up[up[x][i-1]][i-1];}LCAprec(x);}}}
*/

int main()
{
fast
int t=1;
//cin>>t;
for(int tc=1;tc<=t;tc++)
{
   ll n,m;
   cin>>n>>m;
   vll v(n);
   for(int i=0;i<n;i++)
   {
       cin>>v[i];
   }
   for(int i=0;i<m;i++)
   {
      ll x,y;
      cin>>x>>y;
      adj[x].pb(y);
   }
   set<ll>s;
   s.insert(v.back());
   ll cnt=0;
   for(int i=n-2;i>=0;i--)
   {
    ll cnt1=0;
       for(auto x:adj[v[i]])
       {
          if(s.count(x)) cnt1++;
       }
       if(cnt1==s.size()) cnt++;
       else 
       {
           s.insert(v[i]);
       }
   }
   cout<<cnt<<'\n';
}


}


Comments

Submit
0 Comments
More Questions

771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion